package cn.kgc.eduservice.controller.front;

import cn.kgc.commonutils.JwtUtils;
import cn.kgc.commonutils.R;
import cn.kgc.commonutils.ordervo.CourseOrder;
import cn.kgc.eduservice.client.OrdersClient;
import cn.kgc.eduservice.entity.EduCourse;
import cn.kgc.eduservice.entity.EduTeacher;
import cn.kgc.eduservice.entity.chapter.ChapterVo;
import cn.kgc.eduservice.entity.frontvo.CourseFrontVo;
import cn.kgc.eduservice.entity.frontvo.CourseWebVo;
import cn.kgc.eduservice.service.EduChapterService;
import cn.kgc.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CourseFrontController
 * @Description: TODO
 * @Author :郭建宾
 * @Date 2020/7/16
 * @Version V1.0
 **/
@Api(tags="前台课程")
@RestController
@RequestMapping("/eduservice/coursefront")
//@CrossOrigin
public class CourseFrontController {
    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private OrdersClient ordersClient;
    //条件查询带分页查询课程
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable Long page, @PathVariable Long limit,
                                @RequestBody(required = false)CourseFrontVo courseFrontVo){
        Page<EduCourse> pageCourse = new Page<>(page, limit);
        Map<String,Object> map = courseService.getCourseFrontList(pageCourse,courseFrontVo);
        //返回分页所有数据
        return  R.ok().data(map);
    }
    //课程详情的方法
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
        //根据课程id， 编写sql语句查询课程信息
       CourseWebVo courseWebVo= courseService.getBaseCourseInfo(courseId);
        //根据课程id 查询章节和小节
        List<ChapterVo> chapterVoList = chapterService.getChapterVIdeoByCourseId(courseId);

        //根据课程id和用户id查询当前课程是否已经支付过了
        boolean buyCourse = ordersClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVoList).data("isBuy",buyCourse);
    }
    //根据课程id 查询课程信息
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseOrder getCourseInfoOrder(@PathVariable String id){
        CourseWebVo courseInfo = courseService.getBaseCourseInfo(id);
        CourseOrder courseOrder = new CourseOrder();
        BeanUtils.copyProperties(courseInfo,courseOrder);
        return courseOrder;

    }
}
